package model;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import database.Database;

public class Meeting extends Appointment {

	/*
	 * REGION private fields
	 */
	private ArrayList<Person> participants; 
	private Room room;

	/*
	 * REGION public constructors
	 */
	public Meeting(int appointmentID) {
		super(appointmentID);
		participants = new ArrayList<Person>();
	}
	
	public Meeting(int appointmentID, int roomID) {
		super(appointmentID);
		setRoom(roomID);
		participants = new ArrayList<Person>();
	}
	
	public Meeting(String title, Date date, int fromPointOfTime, int toPointOfTime, int ownerID, int roomID, ArrayList<Person> participants) {
		super(title, date, fromPointOfTime, toPointOfTime, "", ownerID);
		setRoom(roomID);
		this.participants = participants;
	}
	public Meeting(String title, Date date, int fromPointOfTime, int toPointOfTime, int ownerID, int roomID) {
		super(title, date, fromPointOfTime, toPointOfTime, "", ownerID);
		setRoom(roomID);
		this.participants = new ArrayList<Person>();
	}
	
	
	/*
	 * REGION public getters
	 */
	public ArrayList<Person> getParticipants() {
		return participants;
	}
	
	public int getParticipantsCount() {
		return participants.size() + 1;
	}
	
	public Room getRoom() {
		return room;
	}
	
	public int getRoomID() {
		return room.getID();
	}
	
	/*
	 * REGION public setters
	 */
	public void setRoom(Room room) {
		
		if (room != null) {
			this.room = room;
		}
		else {
			this.room = new Room(1);
		}
	}
	
	public void setRoom(int roomID) {
		this.room = new Room(roomID);
	}
	
	/*
	 *  REGION public methods
	 */
	public void addParticipant(Person participant) {
		if (participant != null && !participants.contains(participant) && participant != owner) {
			participants.add(participant);
		}
	}
	
	public void removeParticipant(Person participant) {
		if (participants.contains(participant)) {
			participants.remove(participant);
		}
	}
	
	public void Cancel() {
		super.Cancel();
		
		// Establish a database connection
		Database database = new Database();
		database.connectToDatabase();
		
		database.deleteMeeting(ID);
		
		database.disconnectDatabase();
	}
	
	public void Load() {
		Database database = new Database();
		database.connectToDatabase();
		
		try {
			ResultSet rs = database.getMeeting(ID);
			
			if (rs.next()) {
				int roomID = rs.getInt("roomID");
				setRoom(new Room(roomID));
			}
			
			rs = database.getAllParticipant(ID);
			
			while (rs.next()) {
				
				// Create Person instances and add these to the ArrayList
				int userID = rs.getInt("userID");
				Person participant = new Person(userID);
				addParticipant(participant);
			}		
		}
		catch(SQLException e) {
			e.printStackTrace();
		}		
		database.disconnectDatabase();		
	}
	
	public void Save() {
		
		// Establish a connection to the database
		Database database = new Database();
		database.connectToDatabase();
		
		if (ID == 0) {
			database.addAppointment(super.getTitle(), super.getDate().toString(), super.getFromPointOfTime().toString(), super.getToPointOfTime().toString(), super.getLocation(), owner.getID());
			
				ID = database.getLatestAppointmentId();
				System.out.println(ID);
				database.addMeeting(ID, getRoomID());
			
		}
		else {try{
			database.updateAppointment(ID, getTitle(), getDate().toString(), getFromPointOfTime().toString(), getToPointOfTime().toString(), getLocation(), owner.getID());
			database.updateMeeting(ID, getRoomID());}catch(NullPointerException e){
				e.printStackTrace();
			}
		}
		for(int i = 0; i<participants.size(); i++){
			database.addInvitation(ID, participants.get(i).getID());
		}
	}
	
	public boolean isOwner(Person person) {
		if (owner == person) {
			return true;
		}
		return false;
	}
	
	public String toString() {
		
		String returnString = super.toString() + ";" + room.getName();  
		
		for (Person participant : participants) {
			returnString += ";" + participant.getID();
		}
		
		return returnString;
	}
	public void printInvs(){
		Database db = new Database();
		db.connectToDatabase();
		try{
			ResultSet rs = db.getInvitedUsers(ID);
			while(rs.next()){
				Person user = new Person(rs.getInt("UserID"));
				System.out.println(user.getFirstName()+" "+user.getLastName());
			}
			System.out.println("fant, eller fant ikke noe");
		}catch(SQLException e){
			e.printStackTrace();
		}
		
	}
	public void deleteMeeting(){
		Database db = new Database();
		db.connectToDatabase();
		int hId = getID(); 
		for(int i = 0; i<participants.size(); i++){
			int userId = participants.get(i).getID();
			int aId = db.appointmentGetIDFromEverything(getTitle(), getDate().toString(), getFromPointOfTime().toString(), getToPointOfTime().toString(), getLocation(), userId);
			db.deleteAppointment(aId);
			db.deleteInvitationsOnAppointmentId(aId);
			db.deleteMeeting(aId);
			
		}
		db.deleteInvitationsOnAppointmentId(hId);
		db.deleteMeeting(hId);
		
				
			
		
	}
	
}
